<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="login-header">
        <h2 class="login-title">幻兽帕鲁开服管理器</h2>
        <p class="login-subtitle">请登录您的账户</p>
      </div>

      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-width="0px" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="User"
            :autofocus="true"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        <el-form-item class="remember-me">
          <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
          <el-link type="primary" class="forgot-password">忘记密码?</el-link>
        </el-form-item>
        <el-form-item class="login-button">
          <el-button type="primary" @click="handleLogin" class="w-full">登录</el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer">
        <p>© 2023 幻兽帕鲁开服管理器. 保留所有权利.</p>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElForm } from 'element-plus';

const router = useRouter();
const loginForm = reactive({
  username: '',
  password: '',
  rememberMe: false
});

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 32, message: '密码长度在 6 到 32 个字符', trigger: 'blur' }
  ]
};

const loginFormRef = ref<InstanceType<typeof ElForm>>();

const handleLogin = async () => {
  if (!loginFormRef.value) return;

  try {
    await loginFormRef.value.validate();
    // 模拟登录请求
    setTimeout(() => {
      ElMessage.success('登录成功');
      router.push('/dashboard');
    }, 800);
  } catch (error) {
    ElMessage.error('请输入正确的用户名和密码');
  }
};
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  border-radius: 12px;
  overflow: hidden;
}

.login-header {
  text-align: center;
  padding: 30px 20px 10px;
}

.login-title {
  margin: 0 0 10px;
  font-size: 24px;
  font-weight: 600;
  color: #1a1a1a;
}

.login-subtitle {
  margin: 0;
  font-size: 14px;
  color: #666;
}

.login-form {
  padding: 20px 30px 10px;
}

.remember-me {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.forgot-password {
  font-size: 13px;
}

.login-button {
  margin-top: 20px;
}

.login-footer {
  text-align: center;
  padding: 15px 20px 25px;
  font-size: 12px;
  color: #999;
}

.w-full {
  width: 100%;
}
</style>